#ifndef _VM_
#define _VM_ 1

#include "results.h"
#include "table.h"

class VM {
public:
    VM(const char* filename): table{filename} {}
    ExecuteResult execute(Statement& statement) {
        switch (statement.type)
        {
        case StatementType::Insert:
            return execute_insert(statement);
        
        case StatementType::Select:
            return execute_select();
        case StatementType::BTree:
            return execute_BTree();
        }
        return ExecuteResult::Unexpected;
    }
private:
    ExecuteResult execute_insert(Statement& statement) {
        return table.insert(statement.row_to_insert, 
                            table.find(statement.row_to_insert.id), 
                            statement.row_to_insert.id);
    }
    ExecuteResult execute_select() {
        if (table.isEmpty()) return ExecuteResult::TableEmpty;
        printf("TODO!\n");
        exit(EXIT_FAILURE);
        Cursor c = table.begin();
        while(!c.endOfTable()) {
            table.select(c)->print();
            c.advance();
        }
        return ExecuteResult::Success;
    }
    ExecuteResult execute_BTree() {
        table.printBTree();
        return ExecuteResult::Success;
    }
private:
    Table table;
};

#endif